#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=c:\users\justin\desktop\creat-vhost\vhost-gui.kxf
$Form1 = GUICreate("Create a new vHost for localdev", 401, 201, 207, 142)
GUISetBkColor(0xFFFFFF)
$website = GUICtrlCreateInput("", 16, 40, 361, 21)
GUICtrlSetCursor (-1, 2)
$website_label = GUICtrlCreateLabel("Website Name ( ex. graphtek.com )", 16, 16, 171, 17)
$done = GUICtrlCreateButton("Done", 304, 160, 73, 33)
$coldfusion = GUICtrlCreateCheckbox("Is this a coldfusion site?", 16, 72, 281, 20)
$directory_edit = GUICtrlCreateButton("Edit Directories", 16, 160, 105, 33)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

If FileExists("test.ini") Then ;Check if the file directory exist.
   IniRead("test.ini", "section1", "key1", "NotFound") ;Read the ini file.

Else
   IniWrite("test.ini", "vHost", "dicretory", "C:\projects\")
EndIf

While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			Exit
		Case $done ;the case when you click on botton
			doneClick()
		 Case $directory_edit ;the case when you click on directory edit
			directoryClick()
	EndSwitch
WEnd

Func directoryClick()
   
EndFunc

Func doneClick()
   $sitename = StringRegExpReplace(GUICtrlRead($website), "(.*)\..*", "$1")
   $fullurl = GUICtrlRead($website)
; Creates the new conf file
   Local $vhostfile = FileOpen("conf.txt", 2)
; Check if file opened for writing OK
   If $vhostfile = -1 Then
	  MsgBox(0, "Error", "Unable to open file.")
	  Exit
   EndIf
	  FileWriteLine($vhostfile, '<VirtualHost *:80>')
	  FileWriteLine($vhostfile, '	ServerName ' & $sitename & '.localdomain')
	  FileWriteLine($vhostfile, '	ServerAdmin ddrake@graphtek.com')
	  FileWriteLine($vhostfile, '	DocumentRoot "C:\Projects\' & $fullurl & '"')
   If GUICtrlRead($coldfusion) = 1 Then
	  FileWriteLine($vhostfile, '')
	  FileWriteLine($vhostfile, '	RewriteEngine on')
	  FileWriteLine($vhostfile, '	RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -d')
	  FileWriteLine($vhostfile, '	RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} [^/]$')
	  FileWriteLine($vhostfile, '	RewriteRule ^(.+) $1/ [R]')
	  FileWriteLine($vhostfile, '')
	  FileWriteLine($vhostfile, '	RewriteCond %{REQUEST_FILENAME} !/(user_images|images|jscalendar-1.0|xinha|fonts)/')
	  FileWriteLine($vhostfile, '	RewriteCond %{IS_SUBREQ} false')
	  FileWriteLine($vhostfile, '	RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d')
	  FileWriteLine($vhostfile, '	RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f')
	  FileWriteLine($vhostfile, '	RewriteRule ^/(.+)$ /index.cfm/$1 [PT]')
   EndIf
	  FileWriteLine($vhostfile, '</VirtualHost>')
	  FileClose($vhostfile)
   
; Edits the host file.
	Local $hostfile = FileOpen("host.txt", 1)

; Check if file opened for writing OK
	If $hostfile = -1 Then
	MsgBox(0, "Error", "Unable to open file.")
	Exit
	EndIf

	FileWriteLine($hostfile, '127.0.0.1	'& $sitename & '.local')
	FileClose($hostfile)
	
 EndFunc